package com.mmall.util;

import com.mmall.common.RedisShardedPool;
import com.mmall.common.RedisShardedPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisShardedPoolUtil {
    private static Logger log = LoggerFactory.getLogger(RedisShardedPoolUtil.class);

    /**
     * 设置值
     *
     * @param key
     * @param val
     * @return
     */
    public static String set(String key, String val) {
        String resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.set(key, val);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("set key:{} val:{} error ;{}", key, val, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }

    /**
     * 获取旧值设置新值
     * @param key
     * @param val
     * @return
     */
    public static String getSet(String key, String val) {
        String resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.getSet(key, val);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("getSet key:{} val:{} error ;{}", key, val, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }


    /**
     * 设置值不存在才能设置成功
     *
     * @param key
     * @param val
     * @return
     */
    public static Long setNx(String key, String val) {
        Long resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.setnx(key, val);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("setNx key:{} val:{} error ;{}", key, val, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }

    /**
     * 重置有效期
     *
     * @param key
     * @param exTime 单位 秒
     * @return
     */
    public static Long exPire(String key, Integer exTime) {
        Long resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.expire(key, exTime);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("expire key:{} ex:{} error ;{}", key, exTime, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }


    /**
     * 设置有效期的值
     *
     * @param key
     * @param val
     * @param exTime 单位秒
     * @return
     */
    public static String setEx(String key, String val, Integer exTime) {
        String resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.setex(key, exTime, val);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("setex key:{} val:{} ex:{} error ;{}", key, val, exTime, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        String resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.get(key);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("get key:{} error ;{}", key, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }

    /**
     * 删除keys
     *
     * @param key
     * @return
     */
    public static Long del(String key) {
        Long resule = null;
        ShardedJedis jedis = null;
        try {
            jedis = RedisShardedPool.getjedis();
            resule = jedis.del(key);
        } catch (Exception e) {
            RedisShardedPool.returnBrokenResource(jedis);
            log.error("get key:{} error ;{}", key, e);
            return resule;
        }
        RedisShardedPool.retrunResource(jedis);
        return resule;
    }


}
